package it.mp.acquaalta.widget;

import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceClickListener;

public class Setup extends PreferenceActivity {
	private int mAppWidgetId = 0;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		if (extras != null) {
			mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
		}

		addPreferencesFromResource(R.xml.preference);
		findPreference("FINITO").setOnPreferenceClickListener(new OnPreferenceClickListener() {

			public boolean onPreferenceClick(Preference preference) {
				Intent resultValue = new Intent();
				resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
				setResult(RESULT_OK, resultValue);
				startServiceUpdate();				
				finish();
				return true;
			}

		});

	}
	private void startServiceUpdate() {
		Intent svc = new Intent(this, AcquaAltaService.class);
		svc.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
		startService(svc);
	}

}
